Linux下flash操作读、写、擦除步骤 |
您所在的位置:网站首页 › Linux flashrom 按地址擦除 › Linux下flash操作读、写、擦除步骤 |
1、 背景介绍 在板上,ZYNQ PL部分通过EMC连接一片NOR FLASH,地址空间如下:
可以看到NOR FLASH的起始地址为0x80000000,这是物理地址,可以把数据存放在以该地址起始的一段区域。 需要注意的是,在对NOR FLASH进行读写数据时,需要参考对应的datasheet,例如这里选用的NOR FLASH读、写、擦除步骤如下:
通过上面的表格就知道进行相应操作每一步该做什么,可以转换为SDK中裸奔程序的C代码。 2、 Linux下flash操作 之前提到过zynq中Linux用户应用程序可以通过/dev/mem访问到物理地址,xil_in32和xil_out32等裸奔程序中常见的函数可以通过这一机制移植到linux下。于是,对flash的操作其实就是基于xil_in和xil_out对物理地址进行读写。 这里只需要实现三个函数,erase,write_sector以及read_sector.代码如下: //xil_io.h #include #include #include #include #include #include #include #include #include #define PAGE_SIZE ((size_t)getpagesize()) #define PAGE_MASK ((uint64_t) (long)~(PAGE_SIZE - 1)) void Xil_Out32(uint64_t phyaddr, uint32_t val) { int fd; volatile uint8_t *map_base; uint64_t base = phyaddr & PAGE_MASK; uint64_t pgoffset = phyaddr & (~PAGE_MASK); if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) { perror("open /dev/mem:"); } map_base = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, base); if(map_base == MAP_FAILED) { perror("mmap:"); } *(volatile uint32_t *)(map_base + pgoffset) = val; close(fd); munmap((void *)map_base, PAGE_SIZE); } int Xil_In32(uint64_t phyaddr) { int fd; uint32_t val; volatile uint8_t *map_base; uint64_t base = phyaddr & PAGE_MASK; uint64_t pgoffset = phyaddr & (~PAGE_MASK); //open /dev/mem if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) { perror("open /dev/mem:"); } //mmap map_base = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, base); if(map_base == MAP_FAILED) { perror("mmap:"); } val = *(volatile uint32_t *)(map_base + pgoffset); close(fd); munmap((void *)map_base, PAGE_SIZE); return val; } void Xil_Out16(uint64_t phyaddr, uint16_t val) { int fd; volatile uint8_t *map_base; uint64_t base = phyaddr & PAGE_MASK; uint64_t pgoffset = phyaddr & (~PAGE_MASK); if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) { perror("open /dev/mem:"); } map_base = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, base); if(map_base == MAP_FAILED) { perror("mmap:"); } *(volatile uint16_t *)(map_base + pgoffset) = val; close(fd); munmap((void *)map_base, PAGE_SIZE); } int Xil_In16(uint64_t phyaddr) { int fd; uint16_t val; volatile uint8_t *map_base; uint64_t base = phyaddr & PAGE_MASK; uint64_t pgoffset = phyaddr & (~PAGE_MASK); //open /dev/mem if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) { perror("open /dev/mem:"); } //mmap map_base = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, base); if(map_base == MAP_FAILED) { perror("mmap:"); } val = *(volatile uint16_t *)(map_base + pgoffset); close(fd); munmap((void *)map_base, PAGE_SIZE); return val; } void Xil_Out8(uint64_t phyaddr, uint16_t val) { int fd; volatile uint8_t *map_base; uint64_t base = phyaddr & PAGE_MASK; uint64_t pgoffset = phyaddr & (~PAGE_MASK); if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) { perror("open /dev/mem:"); } map_base = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, base); if(map_base == MAP_FAILED) { perror("mmap:"); } *(volatile uint8_t *)(map_base + pgoffset) = val; close(fd); munmap((void *)map_base, PAGE_SIZE); }
|
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |